package com.hrg.rds.biz.config;

import cn.hutool.core.collection.CollectionUtil;
import com.hrg.rds.biz.robot.domain.AgvVo;
import com.hrg.rds.biz.robot.service.AgvService;
import com.hrg.rds.biz.station.domain.Segment;
import com.hrg.rds.biz.station.domain.Station;
import com.hrg.rds.biz.station.service.StationService;
import com.hrg.rds.biz.traffic.TrafficZone;
import com.hrg.rds.biz.traffic.TrafficZoneVo;
import com.hrg.rds.biz.waybill.service.WaybillInstanceService;
import com.hrg.seer.sdk.rbk.RbkClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Configuration
public class RdsConfiguration {

    public static final String ControlRightsName = "HRG-RDS";
    public static final String SelfPosition = "SELF_POSITION";
    public static final int PushedIntervalMilliseconds = 200;
    public static final int PushedExpireSeconds = PushedIntervalMilliseconds / 100 ;

    public static final List<String> IncludedFields = CollectionUtil.toList(
    "x","y","angle","current_station","vx","vy",
            "blocked","battery_level","charging","emergency","fatals","errors",
            "current_map","vehicle_id","create_on","brake","soft_emc","current_lock",
            "confidence","loc_state","is_stop","block_reason","block_id",
            "slowed", "slow_reason","task_status","task_type","target_id","target_point",
            "finished_path", "unfinished_path","fork","roller","jack","hook",
            "yaw", "pitch","pgvs","manual_charge","auto_charge","electric",
            "ssid", "rssi","ap_addr","loc_method"
    );
    public static Map<String, RbkClient> AgvIpClientMap = new ConcurrentHashMap<>();
    
    // 车辆的实时数据，
    public static Map<String, AgvVo> AgvIpMap = new ConcurrentHashMap<>();
    public static Map<String, TrafficZoneVo> TrafficZoneMap = new ConcurrentHashMap<>();

    public static Map<String, Station> StationCodeMap = new ConcurrentHashMap<>();

    public static Map<String, Segment> SegmentMap = new ConcurrentHashMap<>();

    // 重启后，运单数据是否就绪；agv的交管资源状态是否就绪;？
    public static boolean IfWaybillReadyOnRestart = false;

    // 重启后，推送agv数据是否就绪（电量、急停、错误、控制权）;
    // 最近一次收到推送的时间超时后不可用
    public static volatile boolean IfAgvReadyOnReconnect = false;

    // 0: 占分段，1：占子任务，//暂不支持2：占固定数量点。
    public static int OccupiedResourceLevel;

    public static int OccupiedAheadStationNum = 5;
    
    @Autowired
    private AgvService agvService;
    
    @PostConstruct
    public void init() {
        
        // 遍历agv表查询出来的数据，初始化连接client
        List<AgvVo> agvVos = agvService.selectList();
        for (AgvVo agvVo : agvVos) {
            String host = agvVo.getIp();
            if (agvVo.isIfEnabled()) {
                AgvIpMap.put(host, agvVo);
                AgvIpClientMap.put(host, new RbkClient(host));
            }
        }
        AgvIpClientMap.values().forEach(RbkClient::initClient);
    }
}
